﻿using Microsoft.EntityFrameworkCore;
using XsqTech.SmartOA.Domain.Table;
using XsqTech.SmartOA.Dtos.DTO;
using XsqTech.SmartOA.EFCore.Respository.Base;
using XsqTech.SmartOA.IRespository;

namespace XsqTech.SmartOA.EFCore.Respository
{
    /// <summary>
    /// 部门模块-仓储实现类
    /// </summary>
    public class DepartmentRespository : BaseRespository<Department>, IDepartmentRespository
    {
        DbContext _db;
        /// <summary>
        /// 构造方法
        /// </summary>
        public DepartmentRespository(DbContext db) : base(db)
        {
            _db = db;
        }

        /// <summary>
        /// 部门添加用户
        /// </summary>
        /// <param name="depId"></param>
        /// <param name="uId"></param>
        /// <returns></returns>

        public int AddDepartmentsUser(Guid DepId, string Id)
        {
            using (var tran = _db.Database.BeginTransaction())
            {
                try
                {
                    int affectRows = _db.Database.ExecuteSqlInterpolated($"delete from DepartmentUser where DepId={DepId}");

                    Id.Split(',').ToList().ForEach(userId =>
                    {
                        affectRows += _db.Database.ExecuteSqlInterpolated(
                                                $"insert into DepartmentUser values('061FD78B-2132-4C9B-FC7C-08DB2C63EBE4',{DepId},{userId})"
                                            );
                    });

                    tran.Commit();
                    return affectRows; //返回受影响行数
                }
                catch (Exception ex)
                {
                    //记录日志
                    tran.Rollback();
                    return -1;
                }
            }
        }
        /// <summary>
        /// 部门添加
        /// </summary>
        /// <param name="department"></param>
        /// <returns></returns>

        public MsgResponse<AddDepartmentsDto> DepartmentRespositoryAdd(AddDepartmentsDto department)
        {
            Department departments = new Department()
            {
                Name = department.Name,
                Status = department.Status,
            };
            //添加
            _db.Add(departments);
            //实例化结果返回模型
            int res = _db.SaveChanges();
            if (res > 0)
            {
                MsgResponse<AddDepartmentsDto> responseModel = new MsgResponse<AddDepartmentsDto>()
                {
                    Code = 200,
                    Msg = "添加成功!",
                    Data = department,
                };
                return responseModel;
            }
            else
            {
                MsgResponse<AddDepartmentsDto> responseModel = new MsgResponse<AddDepartmentsDto>()
                {
                    Code = 401,
                    Msg = "添加失败!",
                };
                return responseModel;
            }
        }
    }
    public class DepartmentUserRespository : BaseRespository<DepartmentUser>, IDepartmentUserRespository
    {
        public DepartmentUserRespository(DbContext db) : base(db)
        {
        }
    }
}
